
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace SOEI.Solution.DomainEntities.CustomerManagement.DomainService
{
    /// <summary>
    /// 领域服务层一个模块的核心业务逻辑
    ///</summary>
    public class SellerManager : DomainServiceBase, ISellerManager
    {

        private readonly IRepository<Seller, long> _sellerRepository;

        /// <summary>
        /// 【Seller】的构造方法
        /// 通过构造函数注册服务到依赖注入容器中
        ///</summary>
        public SellerManager(IRepository<Seller, long> sellerRepository)
        {
            _sellerRepository = sellerRepository;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        /// 返回列表查询用
        /// </summary>
        /// <returns></returns>
        public IQueryable<Seller> QueryEntityListAsNoTracking()
        {

            var query = _sellerRepository.GetAll().AsNoTracking()
                        .Select(x => new Seller
                        {

                            Name = x.Name,
                            Sex = x.Sex,
                            IdNumber = x.IdNumber,
                            PhoneNumber = x.PhoneNumber,
                            HouseTitleNumber = x.HouseTitleNumber,
                            HousingLocation = x.HousingLocation,
                            SellingPrice = x.SellingPrice,
                            HousePurpose = x.HousePurpose,
                            HouseArea = x.HouseArea,
                            UserTag = x.UserTag,
                            Remark = x.Remark,
                        });
            return query;
        }

        /// <summary>
        /// 【Seller】返回表达式数的实体信息即IQueryable类型
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<Seller> Query()
        {
            return _sellerRepository.GetAll();
        }
        /// <summary>
        /// 【Seller】返回即IQueryable类型的实体，不包含EF Core跟踪标记
        /// </summary>
        /// <returns>IQueryable</returns>
        public IQueryable<Seller> QueryAsNoTracking()
        {
            return _sellerRepository.GetAll().AsNoTracking();
        }
        /// <summary>
        /// 【Seller】根据Id查询实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>实体</returns>
        public async Task<Seller> FindByIdAsync(long id)
        {
            var entity = await _sellerRepository.GetAsync(id);
            return entity;
        }
        /// <summary>
        /// 【Seller】检查实体是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns>bool</returns>
        public async Task<bool> IsExistAsync(long id)
        {
            var result = await _sellerRepository.GetAll().AnyAsync(a => a.Id == id);
            return result;
        }
        /// <summary>
        /// 【Seller】创建实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Seller> CreateAsync(Seller entity)
        {
            entity.Id = await _sellerRepository.InsertAndGetIdAsync(entity);
            return entity;
        }
        /// <summary>
        /// 【Seller】更新实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task UpdateAsync(Seller entity)
        {
            await _sellerRepository.UpdateAsync(entity);
        }
        /// <summary>
        /// 【Seller】删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(long id)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _sellerRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 【Seller】批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task BatchDelete(List<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _sellerRepository.DeleteAsync(a => input.Contains(a.Id));
        }


        #endregion


        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展领域服务接口*/
        /// <summary>
        /// 是否存在指定实体
        /// </summary>
        /// <param name="IdNumber">产权人身份证号</param>
        /// <returns></returns>
        public async Task<bool> IsExistAsync(string IdNumber)
        {
            var result = await _sellerRepository.GetAll().AnyAsync(a => a.IdNumber == IdNumber);
            return result;
        }
        #endregion









    }
}
